<?php
/**
 * Created by PhpStorm.
 * User: liuhong
 * Date: 2018/7/18
 * Time: 下午4:30
 */

namespace app\common\controller;
use app\common\model;

use think\Controller;
use think\Exception;
use think\Request;

class Base extends Controller
{

    public $configModel;
    static public $codeSuccess='100';
    static public $codeError='400';
    public function __construct(Request $request = null)
    {
        $this->configModel = new model\Config;
        parent::__construct($request);
        self::_setConfig();

    }
    protected function _setConfig () {
        try{
            $list = $this->configModel->getList();
            foreach ($list as $k=>$v) {
                config($v['key'], $v['value']);
            }
        }catch (Exception $ex){
            $this->ajax(self::$codeError,'异常:'.$ex->getMessage());
        }
    }

    /**
     * @param $code
     * @param $message
     * @param null $data
     */
    protected function ajax($code,$message,$data=null,$dataName=null){
        if($data!=null){
            if($dataName==null){
                echo json_encode(array('code'=>$code,'msg'=>$message,'data'=>$data));
                exit();
            }else{
                echo json_encode(array('code'=>$code,'msg'=>$message,$dataName=>$data));
                exit();
            }
        }else{
            echo json_encode(array('code'=>$code,'msg'=>$message)) ;
            exit();
        }
    }
    
}